package cn.jietuo.juc.c_010_interview3;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2020/5/13
 * @description:
 */
public class T04_cas {

    enum READ_TO_RUN {T1, T2}

    private static volatile READ_TO_RUN read_to_run = READ_TO_RUN.T1;

    static char[] str1 = new char[]{'1', '2', '3', '4', '5', '6', '7', '8'};
    static char[] str2 = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < str1.length; i++) {
                while (read_to_run != READ_TO_RUN.T1) {
                }
                System.out.println(str1[i]);
                read_to_run = READ_TO_RUN.T2;
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < str2.length; i++) {
                while (read_to_run != READ_TO_RUN.T2) {
                }
                System.out.println(str2[i]);
                read_to_run = READ_TO_RUN.T1;
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
